home *** CD-ROM | disk | FTP | other *** search
/ Amiga Tools 5 / Amiga Tools 5.iso / tools / wb-tools / centerwin / centerwin.c < prev    next >
C/C++ Source or Header  |  1996-04-27  |  9KB  |  381 lines

  1. /*
  2. **  $VER: CenterWindow 2.2 (27 Apr 1996)  **
  3. **
  4. **        © 1996 Timo C. Nentwig
  5. **          all rights reserved !
  6. **
  7. ** ======================================
  8. **
  9. **  Language:
  10. **  ¯¯¯¯¯¯¯¯
  11. **
  12. **    Program is compiled by SAS/C
  13. **
  14. **
  15. **  Purpose:
  16. **  ¯¯¯¯¯¯¯
  17. **
  18. **    Center the active window
  19. **    by hotkey.
  20. **
  21. **    Put window under window -> Parent
  22. **    by hotkey.
  23. **
  24. **  Requirements:
  25. **  ¯¯¯¯¯¯¯¯¯¯¯¯
  26. **
  27. **    AmigaOS v37+
  28. **
  29. **  Bugs:
  30. **  ¯¯¯¯
  31. **
  32. **
  33. **
  34. **  ToDo:
  35. **  ¯¯¯¯
  36. **
  37. **
  38. **
  39. **  Notes:
  40. **  ¯¯¯¯¯
  41. **
  42. **
  43. ** ======================================
  44. **
  45. **  History:
  46. **  ¯¯¯¯¯¯¯
  47. **
  48. **  01 Apr 1996 - 1.0 : initial release
  49. **  26 Apr 1996 - 2.0 : completely overworked (name changed)
  50. **                      now works by hotkey
  51. **  27 Apr 1996 - 2.1 : HOTKEY -> CENTERKEY
  52. **                      add: PARENTKEY -> put window under window -> parent
  53. **
  54. **  27 Apr 1996 - 2.2 : PARENTKEY: Care if win -> Parent has WFLG_DRAGBAR
  55. **                      add: Tooltype FULLHEIGHT
  56. **
  57. */
  58.  
  59. /// #include
  60.  
  61. #include <dos/dos.h>
  62. #include <exec/types.h>
  63. #include <intuition/intuition.h>
  64. #include <intuition/intuitionbase.h>
  65.  
  66. #include <libraries/commodities.h>
  67.  
  68. #include <proto/commodities.h>
  69. #include <proto/exec.h>
  70. #include <proto/intuition.h>
  71.  
  72. #include <stdio.h>
  73. #include <strings.h>
  74.  
  75. ///
  76. /// #define
  77.  
  78. #define EVT_CENTERKEY  1L
  79. #define EVT_PARENTKEY  2L
  80.  
  81. #define PRG_VERSION "2.2"
  82. #define PRG_TITLE   "CenterWin"
  83. #define PRG_AUTHOR  "Timo C. Nentwig"
  84. #define PRG_YEAR    "1996"
  85.  
  86. ///
  87. /// Prototypes
  88.  
  89. VOID      ProcessMsg      (VOID);
  90. BOOL      AttachFilter    (STRPTR onkey, ULONG user_event);
  91.  
  92. ///
  93.  
  94. struct    IntuitionBase   *IntuitionBase;
  95. struct    Library         *CxBase;
  96. struct    MsgPort         *broker_mp;
  97. CxObj                     *broker;
  98. ULONG                      cxsigflag;
  99. BOOL                       fullheight;
  100.  
  101. /// main ()
  102.  
  103. VOID
  104. main (UWORD argc, STRPTR *argv)
  105. {
  106.  
  107.     if (IntuitionBase = (struct IntuitionBase *) OpenLibrary ("intuition.library", 37))
  108.     {
  109.  
  110.         if (CxBase = OpenLibrary ("commodities.library", 37))
  111.         {
  112.  
  113.             if (broker_mp = CreateMsgPort())
  114.             {
  115.  
  116.                 STRPTR   *ttypes;
  117.                 STRPTR    centerkey;
  118.                 STRPTR    parentkey;
  119.                 struct    NewBroker    newbroker =
  120.                 {
  121.  
  122.                     NB_VERSION,
  123.                     PRG_TITLE,
  124.                     PRG_TITLE" "PRG_VERSION" © "PRG_YEAR" by "PRG_AUTHOR,
  125.                     "Center window by hotkey",
  126.                     NBU_UNIQUE | NBU_NOTIFY,
  127.                     0, 0, 0, 0
  128.  
  129.                 };
  130.  
  131.                 newbroker.nb_Port = broker_mp;
  132.                 cxsigflag         = 1L << broker_mp->mp_SigBit;
  133.  
  134.                 ttypes            = ArgArrayInit (argc, argv);
  135.                 newbroker.nb_Pri  = (BYTE) ArgInt (ttypes, "CX_PRIORITY", 0);
  136.                 centerkey         =     ArgString (ttypes, "CENTERKEY",   "F1");
  137.                 parentkey         =     ArgString (ttypes, "PARENTKEY",   "F2");
  138.  
  139.                 if (stricmp (ArgString (ttypes, "FULLWIDTH", "NO"), "NO") == 0)
  140.                     fullheight = FALSE;
  141.                 else
  142.                     fullheight = TRUE;
  143.  
  144.                 if (broker = CxBroker (&newbroker, NULL))
  145.                 {
  146.  
  147.                     CxMsg   *msg;
  148.  
  149.                     if (AttachFilter (centerkey, EVT_CENTERKEY) &&
  150.                         AttachFilter (parentkey, EVT_PARENTKEY))
  151.                     {
  152.  
  153.                         ActivateCxObj (broker, 1L);
  154.                         ProcessMsg();
  155.  
  156.                     }
  157.  
  158.                     DeleteCxObjAll (broker);
  159.  
  160.                     while (msg = (CxMsg *) GetMsg (broker_mp))         // Empty the port of all CxMsgs
  161.                         ReplyMsg ((struct Message *) msg);
  162.  
  163.                 }
  164.  
  165.                 DeletePort(broker_mp);
  166.  
  167.             }
  168.  
  169.             ArgArrayDone();
  170.             CloseLibrary (CxBase);
  171.  
  172.         }
  173.         else
  174.         {
  175.  
  176.             printf ("ERROR: Couldn't open commodities.library v37+\n");
  177.  
  178.         }
  179.  
  180.         CloseLibrary ((struct Library *) IntuitionBase);
  181.  
  182.     }
  183.     else
  184.     {
  185.  
  186.         printf ("ERROR: Couldn't open intuition.library v37+\n");
  187.  
  188.     }
  189.  
  190. }
  191.  
  192. ///
  193. /// ProcessMsg ()
  194.  
  195.     /*
  196.      *    FROM        /rkm/libs/commodities/hotkey.c
  197.      *
  198.      *    FUNCTION    Process commodity messages.
  199.      *
  200.      *    NOTE
  201.      *
  202.      *    EXAMPLE     ProcessMsg ();
  203.      *
  204.      */
  205.  
  206.  
  207. VOID
  208. ProcessMsg (VOID)
  209. {
  210.  
  211.     CxMsg   *msg;
  212.     ULONG    sigrcvd;
  213.     ULONG    msgid;
  214.     ULONG    msgtype;
  215.     LONG     returnvalue = 1L;
  216.  
  217.     while (returnvalue)
  218.     {
  219.  
  220.         sigrcvd = Wait (SIGBREAKF_CTRL_C | cxsigflag);
  221.  
  222.         while (msg = (CxMsg *) GetMsg (broker_mp))
  223.         {
  224.  
  225.             msgid   = CxMsgID   (msg);
  226.             msgtype = CxMsgType (msg);
  227.             ReplyMsg ((struct Message *) msg);
  228.  
  229.             switch (msgtype)
  230.             {
  231.  
  232.                 case CXM_IEVENT:
  233.  
  234.                     switch (msgid)
  235.                     {
  236.  
  237.                         case EVT_CENTERKEY:
  238.  
  239.                             {
  240.  
  241.                                 struct    Window   *win = IntuitionBase -> ActiveWindow;
  242.                                 UWORD               plus;
  243.  
  244.                                 if (fullheight)                                                // accept titlebar
  245.                                     plus = win -> Parent -> WScreen -> Font -> ta_YSize + 3;
  246.                                 else
  247.                                     plus = 0;
  248.  
  249.                                 MoveWindow (win, (((win -> WScreen -> Width          - win -> Width ) / 2) - win -> LeftEdge),
  250.                                                 ((((win -> WScreen -> Height - plus) - win -> Height) / 2) - win ->  TopEdge));
  251.  
  252.                             }
  253.                             break;
  254.  
  255.                         case EVT_PARENTKEY:
  256.  
  257.                             {
  258.  
  259.                                 struct    Window   *win = IntuitionBase -> ActiveWindow;
  260.                                 UWORD               plus;
  261.  
  262.                                 if (win -> Parent -> Flags &= WFLG_DRAGBAR)                    // parent has a dragbar
  263.                                     plus = win -> Parent -> WScreen -> Font -> ta_YSize + 3;
  264.                                 else
  265.                                     plus = 0;
  266.  
  267.  
  268.                                 if (win -> Parent)
  269.                                 {
  270.  
  271.                                     MoveWindow (win, win -> Parent -> LeftEdge - win -> LeftEdge,
  272.                                                     (win -> Parent -> TopEdge -  win -> TopEdge) + plus);
  273.  
  274.                                 }
  275.                                 else
  276.                                 {
  277.                                     // put to 0,0
  278.                                     MoveWindow (win, 0 - win -> LeftEdge,
  279.                                                      0 - win ->  TopEdge);
  280.  
  281.                                 }
  282.  
  283.                             }
  284.                             break;
  285.  
  286.                     }
  287.                     break;
  288.  
  289.                 case CXM_COMMAND:
  290.  
  291.                     switch (msgid)
  292.                     {
  293.  
  294.                         case CXCMD_DISABLE:
  295.  
  296.                             ActivateCxObj (broker, 0L);
  297.                             break;
  298.  
  299.                         case CXCMD_ENABLE:
  300.  
  301.                             ActivateCxObj (broker, 1L);
  302.                             break;
  303.  
  304.                         case CXCMD_KILL:
  305.  
  306.                             returnvalue = 0L;
  307.                             break;
  308.  
  309.                         case CXCMD_UNIQUE:
  310.  
  311.                             returnvalue = 0L;
  312.                             break;
  313.  
  314.                     }
  315.                     break;
  316.  
  317.             }
  318.  
  319.         }
  320.  
  321.         if (sigrcvd & SIGBREAKF_CTRL_C)
  322.             returnvalue = 0L;
  323.  
  324.     }
  325.  
  326. }
  327.  
  328. ///
  329. /// AttachFilter ()
  330.  
  331.     /*
  332.      *    FROM        /CloseWB/CloseWB.c
  333.      *
  334.      *    FUNCTION    Attach a filter.
  335.      *
  336.      *    NOTE
  337.      *
  338.      *    EXAMPLE     AttachFilter (hotkey, EVT_HOTKEY);
  339.      *
  340.      */
  341.  
  342.  
  343. BOOL
  344. AttachFilter (STRPTR onkey, ULONG user_event)
  345. {
  346.  
  347.     CxObj   *filter;
  348.     CxObj   *sender;
  349.     CxObj   *translator;
  350.  
  351.  
  352.     if (filter = CxFilter (onkey))
  353.     {
  354.  
  355.         AttachCxObj (broker, filter);
  356.  
  357.        if (sender = CxSender (broker_mp, user_event))
  358.        {
  359.  
  360.            AttachCxObj (filter, sender);
  361.  
  362.            if (translator = CxTranslate (NULL))
  363.            {
  364.  
  365.                AttachCxObj (filter, translator);
  366.  
  367.                if ( ! (CxObjError (filter)))
  368.                    return (TRUE);
  369.  
  370.            }
  371.  
  372.        }
  373.  
  374.     }
  375.  
  376.     return (FALSE);
  377.  
  378. }
  379.  
  380. ///
  381.